package com.wyj.utils

import java.lang.reflect.Field

/**
 * Created by Administrator on 2018/3/8.
 */

object ReflectUtils {
    @Throws(Exception::class)
    fun getStaticClassField(clazz: Class<*>, fieldName: String): Any? {
        val field = clazz.getDeclaredField(fieldName)
        var value: Any? = null
        if (field != null) {
            if (field.isAccessible) {
                value = field.get(null)
            } else {
                field.isAccessible = true
                value = field.get(null)
                field.isAccessible = false
            }
        }
        return value
    }

    @Throws(Exception::class)
    fun getClassInstance(className: String, instanceFieldName: String): Any? {
        val cls = Class.forName(className)
        val field = cls.getField(instanceFieldName)
        var value: Any? = null
        if (field != null) {
            if (field.isAccessible) {
                value = field.get(null)
            } else {
                field.isAccessible = true
                value = field.get(null)
                field.isAccessible = false
            }
        }
        return value
    }

    /**
     * 获取obj对象fieldName的Field
     *
     * @param obj
     * @param fieldName
     * @return
     */
    fun getFieldByFieldName(obj: Any, fieldName: String): Field? {
        var superClass: Class<*>? = obj.javaClass
        while (superClass != Any::class.java) {
            try {
                return superClass?.getDeclaredField(fieldName)
            } catch (e: Exception) {
            }
            superClass = superClass?.superclass
        }
        return null
    }

    /**
     * 获取obj对象fieldName的属性值
     *
     * @param obj
     * @param fieldName
     * @return
     * @throws SecurityException
     * @throws NoSuchFieldException
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     */
    @Throws(Exception::class)
    fun getValueByFieldName(obj: Any, fieldName: String): Any? {
        val field = getFieldByFieldName(obj, fieldName)
        var value: Any? = null
        if (field != null) {
            if (field.isAccessible) {
                value = field.get(obj)
            } else {
                field.isAccessible = true
                value = field.get(obj)
                field.isAccessible = false
            }
        }
        return value
    }

    /**
     * 设置obj对象fieldName的属性值
     *
     * @param obj
     * @param fieldName
     * @param value
     * @throws SecurityException
     * @throws NoSuchFieldException
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     */
    @Throws(Exception::class)
    fun setValueByFieldName(obj: Any, fieldName: String, value: Any) {
        val field = obj.javaClass.getDeclaredField(fieldName)
        if (field.isAccessible) {
            field.set(obj, value)
        } else {
            field.isAccessible = true
            field.set(obj, value)
            field.isAccessible = false
        }
    }

}
